package com.mmall.mmall002.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mmall.mmall002.entity.*;
import com.mmall.mmall002.mapper.CartMapper;
import com.mmall.mmall002.service.CartService;
import com.mmall.mmall002.service.ProductService;
import com.mmall.mmall002.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙菲
 * @since 2021-03-02
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserAddressService userAddressService;


    /**
     * 添加到购物车的方法
     */
    @GetMapping("/add/{productId}/{price}/{quantity}")
    public String add(@PathVariable("productId")Integer productId,
                            @PathVariable("price")float price,
                            @PathVariable("quantity")Integer quantity,
                            HttpSession session,
                            Model model){

        Cart c = new Cart();
        c.setProductId(productId);
        c.setQuantity(quantity);
        c.setCost(price * quantity);

        User u = (User)session.getAttribute("user");
        c.setUserId(u.getId());

        try {
            if(cartService.save(c)){
                return "redirect:/cart/findAllCart";
            }
        } catch (Exception e) {
            model.addAttribute("error","库存不足，请调整购买量!");
            return "forward:/product/findById/"+c.getProductId();
        }
        return null;
    }


    /**
     * 根据用户编号查询用户的购物车信息
     */
    @GetMapping("/findAllCart")
    public ModelAndView findAllCart(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        //获得登录的用户
        User user = (User) session.getAttribute("user");
        modelAndView.setViewName("settlement1");
        if(user == null){
            //根据用户编号查询出来购物车集合
            modelAndView.addObject("cartList",new ArrayList<>());
        }else{
            //根据用户编号查询出来购物车集合
            modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }

    /**
     * 执行删除操作
     */
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id")Integer id){
        cartService.removeById(id);
        return "redirect:/cart/findAllCart";
    }



    /**
     * 跳转到结算界面
     */
    @GetMapping("/settlement2")
    public ModelAndView settlement2(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        //获得登录的用户
        User user = (User) session.getAttribute("user");
        modelAndView.setViewName("settlement2");
        if(user == null){
            //根据用户编号查询出来购物车集合
            modelAndView.addObject("cartList",new ArrayList<>());
        }else{
            //根据用户编号查询出来购物车集合
            modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));

            QueryWrapper qw = new QueryWrapper();
            qw.eq("user_id",user.getId());
            //根据用户编号查询用户的地址信息
            modelAndView.addObject("addressList",userAddressService.list(qw));
        }
        return modelAndView;
    }


    /**
     * 更新购物车中的数据
     */
    @PostMapping("/update/{id}/{quantity}/{cost}")
    @ResponseBody
    public String updateCart(@PathVariable("id")Integer id,
                             @PathVariable("quantity")Integer quantity,
                             @PathVariable("cost")float cost){
        //根据编号查询购物车信息
        Cart c = cartService.getById(id);
        //修改值
        c.setQuantity(quantity);
        c.setCost(cost);

        if(cartService.updateById(c)){
            return "success";
        }else{
            return "fail";
        }

    }



}

